package com.waitingsnow.modules.giftPlay.view.baseView
{
import assets.giftPlay.FaceShapeAsset;
import assets.giftPlay.FishShapeAsset;
import assets.giftPlay.HeartShapeAsset;
import assets.giftPlay.ILoveYouShapeAsset;
import assets.giftPlay.LoveShapeAsset;
import assets.giftPlay.PlaneShapeAsset;
import assets.giftPlay.StarShapeAsset;
import assets.giftPlay.VShapeAsset;

import com.greensock.TweenLite;
import com.greensock.easing.Back;
import com.greensock.easing.Bounce;
import com.greensock.easing.Circ;
import com.greensock.easing.Cubic;
import com.greensock.easing.Elastic;
import com.greensock.easing.Expo;
import com.greensock.easing.Quad;
import com.greensock.easing.Quart;
import com.greensock.easing.Quint;
import com.greensock.easing.Sine;
import com.greensock.plugins.EndArrayPlugin;
import com.greensock.plugins.TweenPlugin;
import com.waitingsnow.core.display.loader.BaseLoader;
import com.waitingsnow.core.model.vo.GiftVO;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.utils.Dictionary;
import flash.utils.clearInterval;
import flash.utils.setTimeout;

/**
 * 
 * @author waitingsnow
 * $Id: NormalGiftPLayView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class NormalGiftPLayView extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>NormalGiftPLayView</code> instance.
	 * 
	 */
	public function NormalGiftPLayView()
	{
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var playRect:Rectangle = new Rectangle(0, 0, 700, 400);
	private var giftVO:GiftVO;
	
	private var keyNumArr:Array = [66, 88, 99, 188, 288, 520, 999, 1314];
	private var totalShapeArr:Array = [];
	
	private var initGiftArr:Array;
	private var curGiftArr:Array;
	
	private var giftBmpDataDict:Dictionary = new Dictionary();
	private var curBmpData:BitmapData;
	public var isRun:Boolean;
	
	private var baseLoader:BaseLoader;
	//0剧型, 1播放
	private var playStatus:int;
	private var curFrame:int;
	private var bitmapPoolArr:Array = [];
	private var intervalID:int;
	
	private var tweenArr:Array = [Back.easeIn, Bounce.easeIn, Circ.easeIn, 
		Cubic.easeIn, Elastic.easeIn, Expo.easeIn, Quad.easeIn, 
		Quart.easeIn, Quint.easeIn, Sine.easeIn];
	
	//==========================================================================
	//  Methods
	//==========================================================================
	public function playGift(giftVO:GiftVO):void
	{
		this.giftVO = giftVO;
		isRun = true;
		clearInterval(intervalID);
		curGiftArr = getShapesArrByCount(giftVO.count);
		getGiftBmpData(giftVO);
	}
	
	private function getGiftBmpData(giftVO:GiftVO):void
	{
		curBmpData = giftBmpDataDict[giftVO.giftID];
		if (curBmpData)
		{
			startPlay();
		}
		else
		{
			if (!baseLoader)
			{
				baseLoader = new BaseLoader();
			}
			baseLoader.startLoad(giftVO.swfPath, null, true);
			baseLoader.addEventListener(Event.COMPLETE, baseLoaderCompleteHandler);
		}
	}
	
	private function startPlay():void
	{
		initGiftArr = getInitGiftArr();
		playStatus = 0;
		curFrame = 0;
		var tweenEase:* = tweenArr[int(Math.random()*tweenArr.length)]
		TweenLite.to(initGiftArr, 3, {endArray:curGiftArr[0], onComplete:onCompleteHandler, ease:tweenEase});
		addEventListener(Event.ENTER_FRAME, enterFrameHandler);
	}
	
	private function onCompleteHandler():void
	{
		TweenLite.delayedCall(0.5, function delayHandler():void
		{
			playStatus = 1;
			intervalID = setTimeout(endPlay, 4000);
		});
	}
	
	private function getInitGiftArr():Array
	{
		var result:Array = [];
		var len:int = curGiftArr[0].length/2;
		for (var i:int = 0; i < len; i++)
		{
			result.push(Math.random()*playRect.width - 200 + 100, Math.random()*playRect.height - 80 + 40);
		}
		return result;
	}
	
	private function getShapesArrByCount(count:int):Array
	{
		var len:int = keyNumArr.length;
		var i:int;
		for (i = 0; i < len; i++)
		{
			if (keyNumArr[i] > count)
			{
				break;
			}
		}
		return totalShapeArr[Math.max(0, i - 1)] as Array;
	}
	
	private function initDisplay():void
	{
		TweenPlugin.activate([EndArrayPlugin]);
		getShapesArr(new VShapeAsset());
		getShapesArr(new StarShapeAsset());
		getShapesArr(new FishShapeAsset());
		getShapesArr(new FaceShapeAsset());
		getShapesArr(new PlaneShapeAsset());
		getShapesArr(new ILoveYouShapeAsset());
		getShapesArr(new LoveShapeAsset());
		getShapesArr(new HeartShapeAsset());
	}
	
	private function getShapesArr(mc:MovieClip):void
	{
		var len:int = mc.totalFrames;
		var result:Array = [];
		for (var i:int = 1; i <= len; i++)
		{
			mc.gotoAndStop(i);
			result.push(recordMcPoints(mc));
		}
		totalShapeArr.push(result);
	}
	
	private function recordMcPoints(mc:MovieClip):Array
	{
		var result:Array = [];
		var len:int = mc.numChildren;
		var display:DisplayObject;
		for (var i:int = 0; i < len; i++)
		{
			display = mc.getChildAt(i);
			result.push(display.x, display.y);
		}
		return result;
	}
	
	private function getBmp(bmpData:BitmapData):Bitmap
	{
		var result:Bitmap;
		if (bitmapPoolArr && bitmapPoolArr.length)
		{
			result = bitmapPoolArr.pop();
		}
		else
		{
			result = new Bitmap();
		}
		result.bitmapData = bmpData;
		return result;
	}
	
	private function collectBitmap(bmp:Bitmap):void
	{
		if (bmp && bmp.parent)
		{
			bmp.parent.removeChild(bmp);
			bmp.bitmapData = null;
		}
		if (bitmapPoolArr)
		{
			bitmapPoolArr.push(bmp);
		}
	}
	
	private function updateView(giftArr:Array):void
	{
		var len:int = giftArr.length / 2;
		removeBitmapByIndex(len);
		var bmp:Bitmap;
		for (var i:int = 0; i < len; i++)
		{
			bmp = getBitmapByIndex(i);
			if (!bmp)
			{
				bmp = getBmp(curBmpData);
				addChild(bmp);
			}
			bmp.x = giftArr[2*i];
			bmp.y = giftArr[2*i + 1];
		}
	}
	
	private function getBitmapByIndex(index:int):Bitmap
	{
		var bmp:Bitmap;
		if (numChildren > index)
		{
			bmp = Bitmap(getChildAt(index));
		}
		return bmp;
	}
	
	private function endPlay():void
	{
		isRun = false;
		removeBitmapByIndex(0);
		dispatchEvent(new Event(Event.COMPLETE));
	}
	
	private function removeBitmapByIndex(index:int):void
	{
		for (var i:int = numChildren - 1; i >= index; i--)
		{
			collectBitmap(Bitmap(getChildAt(i)));
		}
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function baseLoaderCompleteHandler(event:Event):void
	{
		curBmpData = baseLoader.getBmpData(38, 38);
		startPlay();
		giftBmpDataDict[giftVO.giftID] = curBmpData;
	}
	
	private function enterFrameHandler(event:Event):void
	{
		if (playStatus == 0)
		{
			updateView(initGiftArr);
		}
		else if (playStatus == 1)
		{
			if (curFrame < curGiftArr.length)
			{
				updateView(curGiftArr[curFrame]);
				curFrame ++;
			}
			else
			{
				removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			}
		}
	}
}
}